<template>
    <el-container>
        <el-aside width="200px" v-loading="menuloading">
			<el-container>
				<el-header>
					<el-input :placeholder="$t('all.filter')" v-model="menuFilterText" clearable></el-input>
				</el-header>
				<el-main class="nopadding">
					<el-tree ref="menu" class="menu" node-key="id" :data="menuList" :props="menuProps" :current-node-key="''" draggable highlight-current :expand-on-click-node="false" :filter-node-method="menuFilterNode" @node-click="menuClick"></el-tree>
				</el-main>
				<el-footer style="height:51px;">
					<el-button type="primary" size="small" icon="el-icon-plus" v-auth="'bomType.add'" @click="addType"></el-button>
					<el-button type="success" size="small" plain icon="el-icon-edit" v-auth="'bomType.edit'" @click="editMenu"></el-button>
					<el-button type="danger" size="small" plain icon="el-icon-delete" v-auth="'bomType.delete'" @click="delMenu"></el-button>
				</el-footer>
			</el-container>
		</el-aside>
        <el-container>
            <el-header>
                <div class="left-panel">
                    <el-button type="primary" icon="el-icon-plus" v-auth="'bom.add'" @click="add"></el-button>
                    <el-button type="danger" plain icon="el-icon-delete" v-auth="'bom.delete'" :disabled="selection.length == 0" @click="batch_del"></el-button>
                    <!-- <el-button type="primary" plain v-auth="'goods.checkCate'" :disabled="selection.length == 0" @click="changeCate">批量修改分类</el-button> -->
                    <!-- <sc-file-import :apiObj="$API.device.beiJian.goods.import" @success="success" ref="importBox" :templateUrl="'http://2063.mzth.cn/static/goods_import.xls'" v-if="showUp"></sc-file-import> -->
                </div>
                <div class="right-panel">
                    <div class="right-panel-search">
                        <el-input v-model="search.bom_code" :placeholder="'BOM' + $t('all.code')" clearable @keyup.enter="upsearch"></el-input>
                        <el-input v-model="search.goods_code" :placeholder="$t('goods.goods.code')" clearable @keyup.enter="upsearch"></el-input>
                        <el-input v-model="search.goods_name" :placeholder="$t('goods.goods.name')" clearable @keyup.enter="upsearch"></el-input>
                        <el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
                        <el-button type="primary" icon="el-icon-refresh" @click="refresh"></el-button>
                    </div>
                </div>
            </el-header>
            <el-main class="nopadding">
                <scTable ref="table" row-key="id" :apiObj="apiObj" @selection-change="selectionChange" stripe remoteSort remoteFilter border>
                    <el-table-column type="selection" width="50" reserve-selection align="center"></el-table-column>
                    <el-table-column type="index" :label="$t('all.sort')" fixed align="center" width="60"></el-table-column>
                    <el-table-column :label="c" prop="bom_code" align="center"></el-table-column>
                    <el-table-column :label="$t('goods.goods.code')" prop="goods.goods_code" align="center"></el-table-column>
                    <el-table-column :label="$t('goods.goods.name')" prop="goods.goods_name" align="center"></el-table-column>
                    <el-table-column :label="$t('goods.goods.spec')" prop="goods.goods_spec" align="center"></el-table-column>
                    <!-- <el-table-column :label="$t('goods.goods.model')" prop="goods_model" align="center"></el-table-column> -->
                    <el-table-column :label="$t('goods.goods.unit')" prop="goods.unit.unit_name" align="center"></el-table-column>
                    <!-- <el-table-column :label="$t('goods.goods.category')" prop="cate_name" align="center"></el-table-column> -->
                    <el-table-column :label="$t('all.operate')" fixed="right" align="center" width="170">
                        <template #default="scope">
                            <el-button-group>
                                <el-button text type="primary" size="small" @click="table_show(scope.row, scope.$index)" v-auth="'bom.show'">{{$t('all.show')}}</el-button>
                                <el-button text type="primary" size="small" @click="table_edit(scope.row, scope.$index)" v-auth="'bom.edit'">{{$t('all.edit')}}</el-button>
                                <el-popconfirm :title="$t('all.suredel')" @confirm="table_del(scope.row, scope.$index)">
                                    <template #reference>
                                        <el-button text type="danger" size="small" v-auth="'bom.delete'">{{$t('all.delete')}}</el-button>
                                    </template>
                                </el-popconfirm>
                            </el-button-group>
                        </template>
                    </el-table-column>
                </scTable>
            </el-main>
        </el-container>
    </el-container>
	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save = false"></save-dialog>
    <bom-cate v-if="dialog.cate" ref="cateDialog" @success="cateHandleSuccess" @closed="dialog.cate = false"></bom-cate>
</template>

<script>
import BomCate from './bomCate.vue'
import saveDialog from './save'

export default {
	name: 'BOM列表',
	components: {
		saveDialog,BomCate
	},
	data() {
		return {
			dialog: {
				save: false,
				cate: false,
			},
			apiObj: this.$API.goods.bom.list,
			selection: [],
			search: {
				goods_name: null,
                goods_code:null,
				bom_code:null,
				material_code:null
			},
            menuList: [],
			menuProps: {
				label: (data) => {
					return data.cate_name
				}
			},
			menuloading: false,
			menuFilterText: "",
			menuForm: {},
		}
	},
    watch: {
		menuFilterText(val) {
			this.$refs.menu.filter(val);
		}
	},
	mounted() {
		this.getMenu();
	},
	methods: {
		//添加
		add() {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('add')
			})
		},
		//编辑
		table_edit(row) {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('edit').setData(row)
			})
		},
		//查看
		table_show(row) {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('show').setData(row)
			})
		},
		//删除
		async table_del(row, index) {
			var reqData = { id: row.id }
			var res = await this.$API.goods.bom.delete.post(reqData);
			if (res.code == 1) {
				//这里选择刷新整个表格 OR 插入/编辑现有表格数据
				this.$refs.table.tableData.splice(index, 1);
                this.$refs.table.refresh()
				this.$message.success(this.$t('all.delYes'))
			} else {
				this.$alert(res.message, this.$t('all.prompt'), {type: "error"})
			}
		},
		async table_del_all(delete_ids) {
			var reqData = { id: delete_ids }
			var res = await this.$API.goods.bom.delete.post(reqData);
			if (res.code == 1) {
				this.$refs.table.refresh()
				this.$message.success(this.$t('all.delYes'))
			} else {
				this.$alert(res.message, this.$t('all.prompt'), {type: "error"})
			}
		},
		//批量删除
		async batch_del() {
			var delete_ids = [];
			this.selection.forEach((item) => {
				delete_ids.push(item.id)
			})
			this.$confirm(`${this.$t('all.longmessage')} ${this.selection.length} ${this.$t('all.term')}`, this.$t('all.prompt'), {
				type: 'warning'
			}).then(() => {
				this.table_del_all(delete_ids)
			}).catch(() => {

			})
		},
		//表格选择后回调事件
		selectionChange(selection) {
			this.selection = selection;
		},
		//搜索
		upsearch() {
			this.$refs.table.upData(this.search)
		},
        refresh(){
            for (const key in this.search) {
                this.search[key] = null;
            }
            this.$refs.table.upData(this.search)
        },
		//本地更新数据
		handleSuccess(data, mode) {
			if (mode == 'add') {
				this.$refs.table.refresh()
			} else if (mode == 'edit') {
				this.$refs.table.refresh()
			}
		},
        //加载树数据
		async getMenu() {
			this.menuloading = true;
			var res = await this.$API.goods.bomCate.all.get();
			this.menuloading = false;
			var allNode = {id: '', cate_name: this.$t('all.all')}
			res.data.unshift(allNode);
			this.menuList = res.data;
			// this.menuClick(allNode)
		},
		//树点击
		menuClick(data) {
			this.menuForm = data
			var params = {
				cate_id: data.id,
			}
			this.$refs.table.reload(params)
		},
		//树过滤
		menuFilterNode(value, data) {
			if (!value) return true;
			return data.cate_name.indexOf(value) !== -1;
		},
		/**删除菜单*/
		async delMenu() {
			if (!this.menuForm.id) {
				this.$message.warning("请选择需要删除的项")
				return false;
			}
			var confirm = await this.$confirm(this.$t('all.delcate'), this.$t('all.prompt'), {
				type: 'warning',
				confirmButtonText: this.$t('all.delete'),
				confirmButtonClass: 'el-button--danger'
			}).catch(() => {
			})
			if (confirm != 'confirm') {
				return false
			}
			this.menuloading = true
			var reqData = {
				id: this.menuForm.id
			}
			var res = await this.$API.goods.bomCate.delete.post(reqData)
			this.menuloading = false
			if (res.code == 1) {
				this.getMenu()
				this.menuForm = {}
			} else {
				this.$message.warning(res.message)
			}
		},
		/**添加分类*/
		addType() {
			this.dialog.cate = true
			this.$nextTick(() => {
				this.$refs.cateDialog.open('add')
			})
		},
		/**编辑分类*/
		editMenu() {
			if (!this.menuForm.id) {
				this.$message.warning("请选择需要编辑的项")
				return false;
			}
			this.dialog.cate = true
			this.$nextTick(() => {
				this.$refs.cateDialog.open('edit').setData(this.menuForm)
			})
		},
		/**分类添加成功事件*/
		cateHandleSuccess() {
			this.getMenu();
		},
	}
}
</script>

<style></style>
